package com.mall.shopping.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.shopping.IProductService;
import com.mall.shopping.converter.PanelItemConvertor;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author lichao
 * @createdate 2021-01-25 11:15
 */
@Service
@Component
public class IProductServiceImpl implements IProductService {

    /**
     * 商品详情 by lc
     * @param request
     * @return
     */

    @Autowired
    ProductConverter productConverter;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ItemDescMapper itemDescMapper;
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    PanelItemConvertor panelItemConvertor;
    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {

        Long id = request.getId();

        Item item = itemMapper.selectByPrimaryKey(id);
        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
        ProductDetailDto productDetailDto = productConverter.item2DetailDto(item,itemDesc);

        ProductDetailResponse productDetailResponse = new ProductDetailResponse();
        productDetailResponse.setProductDetailDto(productDetailDto);
        return productDetailResponse;

    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {

        PageHelper.startPage(request.getPage(),request.getSize());

        Weekend<Item> weekend = new Weekend<>(Item.class);
        if(!StringUtils.isEmpty(request.getSort())){
            if(request.getSort().equals("1")){
                weekend.orderBy("price").asc();
            }else {
                weekend.orderBy("price").desc();
            }
        }
        WeekendCriteria<Item, Object> weekendCriteria = weekend.weekendCriteria();

        if(request.getPriceGt()!=null){
            weekendCriteria.andGreaterThanOrEqualTo("price",request.getPriceGt());
        }
        if(request.getPriceLte()!=null){
            weekendCriteria.andLessThanOrEqualTo("price",request.getPriceLte());
        }
        weekend.and(weekendCriteria);

        List<Item> items = itemMapper.selectByExample(weekend);
        PageInfo<Item> itemPageInfo = new PageInfo<>(items);

        long total = itemPageInfo.getTotal();

        AllProductResponse allProductResponse = new AllProductResponse();
        allProductResponse.setProductDtoList(productConverter.items2Dto(items));
        allProductResponse.setTotal(total);

        return allProductResponse;
    }

    /**
     * 推荐商品
     * @return
     */
    @Override
    public RecommendResponse getRecommendGoods() {
        //查询panel和panelItem
        Weekend<Panel> panelWeekend = new Weekend<>(Panel.class);
        panelWeekend.createCriteria().andEqualTo("status",1);
        panelWeekend.orderBy("sortOrder").desc();
        List<Panel> panels = panelMapper.selectByExample(panelWeekend);

        for (Panel panel : panels) {
            panel.setPanelContentItems(panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId()));
        }


        //转化为dto

        List<PanelDto> panelDtos1 = panelItemConvertor.panel2Dto(panels);
        Set<PanelDto> panelDtos = new HashSet<>();
        for (PanelDto panelDto : panelDtos1) {
            panelDtos.add(panelDto);
        }
        RecommendResponse recommendResponse = new RecommendResponse();
        recommendResponse.setPanelDtos(panelDtos);
        return recommendResponse;
    }
}
